/* Copyright (c) 2004-2024 peigen.info. All rights reserved. */

package info.peigen.hotpot.business.customer.manage.repository.service;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import info.peigen.hotpot.business.customer.core.domain.partner.Partner;
import info.peigen.hotpot.business.customer.core.repository.entity.PartnerEntity;
import info.peigen.hotpot.business.customer.manage.convertor.PartnerManageConvertor;
import info.peigen.hotpot.business.customer.manage.repository.mapper.PartnerManageMapper;
import info.peigen.hotpot.common.core.result.ResultCode;
import info.peigen.hotpot.common.service.base.exception.BusinessException;
import info.peigen.hotpot.component.data.repository.beetlsql.service.AbstractBeetlSqlWithoutPartnerIdRepository;
import org.noear.solon.annotation.Component;

/**
 * <b>(PartnerManageRepository)</b>
 * 管理后台 "合作伙伴" 仓储服务
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023-09-09
 */
@SuppressWarnings("unused")
@Component
public class PartnerManageRepository extends AbstractBeetlSqlWithoutPartnerIdRepository<PartnerManageMapper, PartnerEntity> {
    public Partner active(String partnerId) {
        Assert.isTrue(StrUtil.isNotEmpty(partnerId), () -> new BusinessException(ResultCode.FAILURE, "激活时[partnerId]不得为空!"));

        PartnerEntity entity = getMapper().findByPartnerId(partnerId);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "激活失败!partnerId=" + partnerId));

        return PartnerManageConvertor.convertToDomain(entity);
    }

    public Partner lock(String partnerId) {
        Assert.isTrue(StrUtil.isNotEmpty(partnerId), () -> new BusinessException(ResultCode.FAILURE, "加锁时[partnerId]不得为空!"));

        PartnerEntity entity = getMapper().lockByPartnerId(partnerId);

        Assert.notNull(entity, () -> new BusinessException(ResultCode.FAILURE, "锁定失败!partnerId为空!"));

        return PartnerManageConvertor.convertToDomain(entity);
    }

    public void removeByPartnerId(String partnerId) {
        Assert.isTrue(StrUtil.isNotEmpty(partnerId), () -> new BusinessException(ResultCode.FAILURE, "删除时[partnerId]不得为空!"));

        getMapper().deleteByPartnerId(partnerId);
    }

    public void store(Partner domain) {
        Assert.isTrue(ObjectUtil.isNotEmpty(domain), () -> new BusinessException(ResultCode.FAILURE, "持久化时[domain]不得为空!"));

        PartnerEntity entity = PartnerManageConvertor.convertToEntity(domain);

        saveOrUpdate(entity);
    }
}